package com.kotlin.demo

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kotlin.demo.bean.ArticleData
import com.kotlin.demo.net.ApiResponse

import com.kotlin.demo.net.RetrofitClient
import kotlinx.coroutines.*
import kotlin.system.measureTimeMillis

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2019-11-19
 * desc   :
 */

class CommViewModel : ViewModel() {

    fun getData() {
        viewModelScope.launch {
            val data = call {
                RetrofitClient.api.getData1()
            }
            //表示有异常
            if (data.code == ApiResponse.ERROR_CODE) {
                Log.i(TAG, ": ${data.msg}")
                return@launch
            }

            //取出我们后台接口返回的数据
            val body = data.body

            body?.let {
                if (it.errorCode == ApiResponse.SUCCESS_CODE) {
                    Log.i(TAG, ": ${it.data}")
                }
            }
        }
    }


    override fun onCleared() {
        super.onCleared()
        viewModelScope.coroutineContext.cancelChildren()
    }

    companion object {
        val TAG: String = CommViewModel::class.java.simpleName
    }
}